/**
 * @ Author: luoqi
 * @ Create Time: 2025-06-29 20:00
 * @ Modified by: luoqi
 * @ Modified time: 2025-11-07 13:18
 * @ Description:
 */

#ifndef _QTERM_H
#define _QTERM_H

#ifdef __cplusplus
extern "C" {
#endif

#include <bsp.h>
#include <string.h>
#include <stddef.h>

#define QCLI_USING_STDSTRING
#include "qcli/qcli.h"

#define QSH(...)                kprintf(__VA_ARGS__)
#define ISARG(str1, str2)       (strcmp(str1, str2) == 0)
#define ISARGV(n, str)          (strcmp(argv[n], #str) == 0)
#define ISARGC(n)               (argc == n)

#define HEX_PRINT(ptr, size)     do{                \
    for(int i = 0; i < size; i++){                  \
        kprintf(" %02X", ptr[i]);                   \
    }                                               \
    kprintf("\r\n");                                \
} while(0)

#define CLI_DEFAULT_CB(name)    __WEAK int name(int argc, char **argv) {UNUSED(argc); UNUSED(argv); return 0; }

#define CLI_ARGS_EXEC(argc, argv, table)                        \
        if(ISARGV(1, ?)) {                                      \
            return cli_help(table, sizeof(table));              \
        } else {                                                \
            return cli_args(argc, argv, table, sizeof(table));  \
        }

typedef QCliCallback    CliCallback;
typedef QCliArgsTable   CliArgsTable;

int cli_init(void);

int cli_title(void);

int cli_exec(char c);

int cli_echo(char *args);

int cli_add(const char *name, CliCallback cb, const char *usage);

int cli_del(const char *name);

int cli_help(CliArgsTable *table, size_t table_size);

inline int cli_args(int argc, char **argv, CliArgsTable *table, size_t table_size)
{
    return qcli_args(argc, argv, table, table_size);
}

#ifdef __cplusplus
}
#endif
#endif
